package com.fzz.netty.study.test;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author fankai
 * @version 1.0
 * @classname WebSocketFrameHandler
 * @description TODO
 * @date 2021/11/02 17:18
 **/
public class WebSocketFrameHandler extends SimpleChannelInboundHandler<Object> {
    Logger logger = LoggerFactory.getLogger(WebSocketFrameHandler.class);


    /**
     * 接收数据
     *
     * @param ctx
     * @param msg
     * @author fankai
     * @date 2021/11/2 17:19
     **/
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        logger.info("接收到消息：{}", msg);
    }


    /**
     *  客户端连接
     * @author fankai
     * @date 2021/11/2 17:22
     * @param ctx
     **/
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        logger.info("客户端连接：{}", ctx.channel().id().asShortText());
    }



    /**
     * 客户端断开
     * @author fankai
     * @date 2021/11/2 17:22
     * @param ctx
     **/
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        logger.info("客户端断开：{}", ctx.channel().id().asShortText());
    }

    /**
     * 读取完成
     * @author fankai
     * @date 2021/11/2 17:23
     * @param ctx
     **/
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }
}
